define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'union/union/index' + location.search,
                    //add_url: 'union/union/add',
                   // edit_url: 'union/union/edit',
                    del_url: 'union/union/del',
                    multi_url: 'union/union/multi',
                    import_url: 'union/union/import',
                    audit_url: 'union/union/audit',//添加的字段
                    audit_arr_url: 'union/union/auditpass',//添加的字段
                    table: 'notice_notice',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE',visible: false},
                        {field: 'mobile_xing', title: __('Mobile'), operate:false},
                        {field: 'reg_mobile', title: __('RegMobile'), operate: 'LIKE',visible: false},
                        {field: 'reg_mobile_xing', title: __('RegMobile'), operate:false},


                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [{name: 'audit', text: '审核', title: '审核', classname: 'btn btn-xs btn-primary btn-dialog', url: 'union/union/audit',disable: function(row){
                                    if (row.status != 0) {
                                        return true;
                                    }
                                }, //判断按钮是否禁用
                            }]
                        }
                    ]
                ]
            });
            // 自定义搜索条件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var that = $(this);

                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    params = queryParams(params);
                    var op =  {};
                    var filter = {};
                    // var op = params.op ? JSON.parse(params.op) : {};
                    // var filter = params.filter ? JSON.parse(params.filter) : {};
                    if(that.data("field")){
                        filter[that.data("field")] = that.data("value");
                        op[that.data("field")] = '=';
                    }
                    params.op = JSON.stringify(op);
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        much: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'union/union/much',
                    import_url: 'union/union/much',
                },
                defaults:{
                    pageList:['All']
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                fixedColumns: true,
                fixedRightNumber: 1,
                pageList:['All'],
                columns: [
                    [
                        {field: 'id', title: __('序号')},
                        {field: 'orderNo', title: __('订单号')},
                        {field: 'pay_money', title: __('金额')},
                        {field: 'memberName', title: __('姓名')},
                        {field: 'idCard', title: __('身份证')},
                        {field: 'phone', title: __('手机')},
                        {field: 'cardNo', title: __('银行卡号')},
                        {field: 'msg', title: __('信息')}
                    ]
                ]
            });
            $("#excelimport").data("upload-success", function(data){
                console.log(data);
            });
            require(['upload'], function (Upload) {
                Upload.api.plupload($(".btn-import"), function (data, ret) {
                    Fast.api.ajax({
                        url: "union/union/much", //上传接收地址
                        data: {file: data.url},
                        success: function (response) {
                            console.log(response);
                            $('#table').bootstrapTable('load', response);
                            layer.closeAll();
                        }
                    }, function (data, ret) {
                        // console.log(data);
                        // console.log(ret);
                    });
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
